# -*- coding: utf-8 -*-
import re
import sys

import coo
import requests
import logging


class SetDispatch:
    cookies = coo.cookies
    headers = coo.headers
    ur = '59.215.206.188'
    dispatch = 'ddglzy'


    def query(self, itemName, regCode):
        url = 'http://{}/{}/dispatch/resourse/itemconfigure/query'.format(self.ur, self.dispatch)
        data = {
            "itemcode": "",
            "itemname": "{}".format(itemName),
            "regName": "",
            "regCode": "{}".format(regCode),
            # "itemname": "房地产开发项目前期物业服务合同备案",
            # "regName": "遵义市",
            # "regCode": "520300",
            "orgName": "",
            "orgCode": "",
            "type": "",
            "_search": "true",
            "nd": "",
            "rows": "10",
            "page": "1",
            "sidx": "sort",
            "sord": "asc",
        }
        x = requests.post(url, data=data, cookies=self.cookies,headers=self.headers).json()
        #print(x)
        itemId = x['rows'][0]['cell']['ID']
        exchangeCode = itemCode = x['rows'][0]['cell']['CODE']
        organName = x['rows'][0]['cell']['ORG_NAME']
        organCode = x['rows'][0]['cell']['ORG_CODE']
        version = x['rows'][0]['cell']['VERSION']
        areaCode = x['rows'][0]['cell']['REGION_CODE']
        areaName = x['rows'][0]['cell']['REGION_NAME']
        itemTime = x['rows'][0]['cell']['AGREE_TIME']
        NAME = x['rows'][0]['cell']['NAME']
        #print(itemId,exchangeCode,itemCode,organName,organCode,version,areaCode,areaName,itemTime,NAME)
        return itemId, exchangeCode, itemCode, organName, organCode, version, areaCode, areaName, itemTime, NAME

    def getID(self,name,regCode):
        head={
            "Host": "{}".format(self.ur),
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "keep-alive",
            "Referer": "http://{}/{}/dispatch/resourse/itemconfigure/index".format(self.ur,self.dispatch),
            "Cookie": "{}".format(self.cookies).encode("utf-8"),
            "Upgrade-Insecure-Requests": "1",
        }
        queryRe=SetDispatch().query(name,regCode)
        #print(queryRe)

        url='http://{}/{}/dispatch/resourse/itemconfigure/itemshow?itemid={}&itemcode={}&version={}'.format(self.ur,self.dispatch,queryRe[0],queryRe[2],queryRe[5])

        x=requests.get(url,cookies=self.cookies,headers=self.headers).text
        match = re.findall(r'value="(.*?)"\s+name="id"', x)
        #print(x)
        if(len(match)==0):
            match=['']
            #print(match,11111111)
            return match[0]
        else:
            #print(match,22222222)
            return match[0]
    def updata(self,name,regCode):
        queryIdRe=SetDispatch().getID(name,regCode)
        #print(queryIdRe)
        queryRe=SetDispatch().query(name,regCode)

        url='http://{}/{}/dispatch/resourse/itemconfigure?method=save'.format(self.ur,self.dispatch)
        备案类事项={"id":"{}".format(queryIdRe),"itemId":"{}".format(queryRe[0]),"itemCode":"{}".format(queryRe[2]),
              "areaCode":"{}".format(queryRe[6]),"areaName":"{}".format(queryRe[7]),"itemTime":"{}".format(queryRe[8]),
              "organName":"{}".format(queryRe[4]),"organCode":"{}".format(queryRe[5]),"exchangeCode":"{}".format(queryRe[1]),
              "sysInfo":  [
        {
            "sysId": "A7875618DB3B46F1993D91BA606B582A",
            "sysType": "dispatch",
            "sysName": "产品调度系统3.5"
        },
        {
            "sysType": "queuing",
            "sysName": ""
        },
        {
            "sysType": "punlish",
            "sysName": ""
        },
        {
            "sysId": "74AAC186781B42099DD1E0639A772E2F",
            "sysType": "online",
            "sysName": "网厅网上申报系统"
        },
        {
            "sysType": "hallManager",
            "sysName": ""
        },
        {
            "sysId": "F2CB256C327B423BB034220F522769AA",
            "sysType": "accept",
            "sysName": "综合受理产品3.5"
        },
                  {
                      "sysId": "9D7FD79D91014AF6A06CBEF4A919D87F",
                      "sysType": "approve",
                      "sysName": "贵州省医疗保障信息系统（备案类事项）"
                  }
    ]}
        二类事项 = {"id": "{}".format(queryIdRe), "itemId": "{}".format(queryRe[0]),
                      "itemCode": "{}".format(queryRe[2]),
                      "areaCode": "{}".format(queryRe[6]), "areaName": "{}".format(queryRe[7]),
                      "itemTime": "{}".format(queryRe[8]),
                      "organName": "{}".format(queryRe[4]), "organCode": "{}".format(queryRe[5]),
                      "exchangeCode": "{}".format(queryRe[1]),
                      "sysInfo": [
                          {
                              "sysId": "A7875618DB3B46F1993D91BA606B582A",
                              "sysType": "dispatch",
                              "sysName": "产品调度系统3.5"
                          },
                          {
                              "sysType": "queuing",
                              "sysName": ""
                          },
                          {
                              "sysType": "punlish",
                              "sysName": ""
                          },
                          {
                              "sysId": "74AAC186781B42099DD1E0639A772E2F",
                              "sysType": "online",
                              "sysName": "网厅网上申报系统"
                          },
                          {
                              "sysType": "hallManager",
                              "sysName": ""
                          },
                          {
                              "sysId": "F2CB256C327B423BB034220F522769AA",
                              "sysType": "accept",
                              "sysName": "综合受理产品3.5"
                          },
                          {
                              "sysId": "893BB2D3393148D0B425C1084A26BA7F",
                              "sysType": "approve",
                              "sysName": "贵州省医疗保障信息系统（二类事项）"
                          }
                      ]}
        备案类=['参保单位参保信息查询',
'参保人员参保信息查询',
'出具《参保凭证》',
'异地安置退休人员备案-登记',
'异地安置退休人员备案-变更',
'异地安置退休人员备案-取消',
'异地长期居住人员备案-登记',
'异地长期居住人员备案-变更',
'异地长期居住人员备案-取消',
'常驻异地工作人员备案-登记',
'常驻异地工作人员备案-变更',
'常驻异地工作人员备案-取消',
'异地转诊人员备案-登记',
'异地转诊人员备案-变更',
'异地转诊人员备案-取消']
        二类=['单位新参保登记',
'单位暂停参保',
'单位注销登记',
'职工参保登记——增加',
'职工参保登记——中断',
'职工参保登记——终止',
'职工参保登记——恢复',
'职工参保登记——在职转退休',
'灵活就业人员基本医疗保险参保登记——增加',
'灵活就业人员基本医疗保险参保登记——中断',
'城乡居民参保登记',
'单位参保信息变更登记',
'职工参保信息变更登记',
'城乡居民暂停参保登记',
'城乡居民参保信息变更登记',
'参保人员个人账户一次性支取',
'转移接续手续办理',
'基本医疗保险参保人员享受门诊慢特病病种待遇认定-申办',
'基本医疗保险参保人员享受门诊慢特病病种待遇认定-变更医疗机构',
'基本医疗保险城乡居民参保人员享受门诊慢特病病种待遇认定-申办',
'基本医疗保险城乡居民参保人员享受门诊慢特病病种待遇认定-变更医疗机构',
'门诊费用报销-普通门诊',
'门诊费用报销-门诊慢特病',
'住院费用报销',
'产前检查费支付',
'生育医疗费支付',
'计划生育医疗费支付',
'生育津贴支付',
'医疗救助对象手工（零星）报销']
        表单=[]
        if name in 二类:
            表单=二类事项
        else:表单=备案类事项
        # date={"id":"C6A3270CF1614ABB9E3A9F8D50299314","itemId":"CCFC7D365E9D4379AFFCB2C42922CF45","itemCode":"11520302009505152U400203611000202","areaCode":"52030201","areaName":"红花岗区中山路街道","itemTime":"5","organName":"红花岗区中山路办事处","organCode":"009505152","exchangeCode":"11520302009505152U400203611000202","sysInfo":[{"sysId":"A7875618DB3B46F1993D91BA606B582A","sysType":"dispatch","sysName":"产品调度系统3.5"},{"sysType":"queuing","sysName":""},{"sysType":"punlish","sysName":""},{"sysId":"74AAC186781B42099DD1E0639A772E2F","sysType":"online","sysName":"网厅网上申报系统"},{"sysType":"hallManager","sysName":""},{"sysId":"F2CB256C327B423BB034220F522769AA","sysType":"accept","sysName":"综合受理产品3.5"},{"sysId":"9D7FD79D91014AF6A06CBEF4A919D87F","sysType":"approve","sysName":"贵州省医疗保障信息系统（备案类事项）"}]}
        try:
            x=requests.post(url,json=表单,cookies=self.cookies,headers=self.headers).json()
        except:sys.exit()
        # print(date)
        # x=requests.post(url,json=date,cookies=self.cookies,headers=self.headers).text
        #print(x)
        返回结果=queryRe[7]+">"+queryRe[-1]+"****"+str(x['statusCode'])
        return 返回结果

    def loop(self):
        返回结果=''
        from datetime import datetime

        # 获取当前日期和时间
        current_datetime = datetime.now()

        # 格式化日期和时间
        formatted_datetime = current_datetime.strftime("%Y-%m-%d %H_%M_%S")+'.txt'
        # 配置日志文件
        logging.basicConfig(filename=str(formatted_datetime), level=logging.INFO)
        item=['单位新参保登记',
'单位暂停参保',
'单位注销登记',
'职工参保登记——增加',
'职工参保登记——中断',
'职工参保登记——终止',
'职工参保登记——恢复',
'职工参保登记——在职转退休',
'灵活就业人员基本医疗保险参保登记——增加',
'灵活就业人员基本医疗保险参保登记——中断',
'城乡居民参保登记',
'单位参保信息变更登记',
'职工参保信息变更登记',
'城乡居民暂停参保登记',
'城乡居民参保信息变更登记',
'参保单位参保信息查询',
'参保人员参保信息查询',
'参保人员个人账户一次性支取',
'出具《参保凭证》',
'转移接续手续办理',
'异地安置退休人员备案-登记',
'异地安置退休人员备案-变更',
'异地安置退休人员备案-取消',
'异地长期居住人员备案-登记',
'异地长期居住人员备案-变更',
'异地长期居住人员备案-取消',
'常驻异地工作人员备案-登记',
'常驻异地工作人员备案-变更',
'常驻异地工作人员备案-取消',
'异地转诊人员备案-登记',
'异地转诊人员备案-变更',
'异地转诊人员备案-取消',
'基本医疗保险参保人员享受门诊慢特病病种待遇认定-申办',
'基本医疗保险参保人员享受门诊慢特病病种待遇认定-变更医疗机构',
'基本医疗保险城乡居民参保人员享受门诊慢特病病种待遇认定-申办',
'基本医疗保险城乡居民参保人员享受门诊慢特病病种待遇认定-变更医疗机构',
'门诊费用报销-普通门诊',
'门诊费用报销-门诊慢特病',
'住院费用报销',
'产前检查费支付',
'生育医疗费支付',
'计划生育医疗费支付',
'生育津贴支付',
'医疗救助对象手工（零星）报销',]
        region={'520300':'遵义市',
# '520302':'红花岗区',
# '52030201':'红花岗区中山路街道',
# '52030202':'红花岗区中华路街道',
# '52030203':'红花岗区舟水桥街道',
# '52030204':'红花岗区巷口镇',
# '52030205':'红花岗区北京路街道',
# '52030206':'红花岗区长征街道',
# '52030207':'红花岗区海龙镇',
# '52030208':'红花岗区金鼎山镇',
# '52030209':'红花岗区老城街道',
# '52030210':'红花岗区南关街道',
# '52030211':'红花岗区迎红街道',
# '52030212':'红花岗区深溪镇',
# '52030213':'红花岗区延安路街道',
# '52030214':'红花岗区忠庄街道',
# '52030215':'红花岗区万里路街道',
# '520303':'汇川区',
# '52030301':'汇川区板桥镇',
# '52030302':'汇川区沙湾镇',
# '52030303':'汇川区芝麻镇',
# '52030304':'汇川区松林镇',
# '52030305':'汇川区毛石镇',
# '52030306':'汇川区山盆镇',
# '52030307':'汇川区泗渡镇',
# '52030308':'汇川区大连路街道',
# '52030309':'汇川区董公寺街道',
# '52030310':'汇川区上海路街道',
# '52030311':'汇川区团泽镇',
# '52030312':'汇川区高坪街道',
# '52030313':'汇川区高桥街道',
# '52030314':'汇川区洗马路街道',
# '520321':'播州区',
# '52032101':'播州区龙坑街道',
# '52032102':'播州区尚嵇镇',
# '52032103':'播州区石板镇',
# '52032104':'播州区新民镇',
# '52032105':'播州区播南街道',
# '52032106':'播州区枫香镇',
# '52032107':'播州区芶江镇',
# '52032108':'播州区洪关乡',
# '52032109':'播州区龙坪镇',
# '52032110':'播州区乐山镇',
# '52032111':'播州区茅栗镇',
# '52032112':'播州区马蹄镇',
# '52032113':'播州区南白街道',
# '52032114':'播州区泮水镇',
# '52032115':'播州区平正乡',
# '52032116':'播州区三岔镇',
# '52032117':'播州区三合镇',
# '52032118':'播州区铁厂镇',
# '52032119':'播州区团溪镇',
# '52032120':'播州区乌江镇',
# '52032121':'播州区西坪镇',
# '52032122':'播州区桂花桥街道',
# '52032123':'播州区影山湖街道',
# '52032124':'播州区鸭溪镇',
# '520322':'桐梓县',
# '520322011':'桐梓县夜郎镇',
# '520322021':'桐梓县楚米镇',
# '52032203':'桐梓县高桥镇',
# '52032204':'桐梓县木瓜镇',
# '52032205':'桐梓县风水镇',
# '52032206':'桐梓县黄莲乡',
# '52032207':'桐梓县尧龙山镇',
# '52032208':'桐梓县羊磴镇',
# '52032209':'桐梓县狮溪镇',
# '52032210':'桐梓县芭蕉镇',
# '52032211':'桐梓县水坝塘镇',
# '52032212':'桐梓县大河镇',
# '52032213':'桐梓县马鬃乡',
# '52032214':'桐梓县小水乡',
# '52032215':'桐梓县九坝镇',
# '52032216':'桐梓县新站镇',
# '52032217':'桐梓县花秋镇',
# '52032218':'桐梓县官仓镇',
# '52032219':'桐梓县燎原镇人民政府',
# '52032220':'桐梓县容光镇',
# '52032221':'桐梓县海校街道',
# '52032222':'桐梓县松坎镇',
# '52032223':'桐梓县娄山关街道',
# '52032224':'桐梓县坡渡镇',
# '52032225':'桐梓县茅石镇',
# '520323':'绥阳县',
# '52032301':'绥阳县洋川街道',
# '52032302':'绥阳县小关乡',
# '52032303':'绥阳县大路槽乡',
# '52032304':'绥阳县郑场镇',
# '52032305':'绥阳县旺草镇',
# '52032306':'绥阳县风华镇',
# '52032307':'绥阳县枧坝镇',
# '52032308':'绥阳县蒲场镇',
# '52032309':'绥阳县黄杨镇',
# '52032310':'绥阳县宽阔镇',
# '52032311':'绥阳县温泉镇',
# '52032312':'绥阳县青杠塘镇',
# '52032313':'绥阳县太白镇',
# '52032314':'绥阳县坪乐镇',
# '52032315':'绥阳县茅垭镇',
# '520324':'正安县',
# '52032401':'正安县瑞濠街道',
# '52032402':'正安县安场镇',
# '52032403':'正安县瑞溪镇',
# '52032404':'正安县杨兴镇',
# '52032405':'正安县凤仪街道',
# '52032406':'正安县碧峰镇',
# '52032407':'正安县新州镇',
# '52032408':'正安县桴焉镇',
# '52032409':'正安县庙塘镇',
# '52032410':'正安县小雅镇',
# '52032411':'正安县和溪镇',
# '52032412':'正安县乐俭镇',
# '52032413':'正安县土坪镇',
# '52032414':'正安县流渡镇',
# '52032415':'正安县谢坝乡',
# '52032416':'正安县市坪乡',
# '52032417':'正安县格林镇',
# '52032418':'正安县班竹镇',
# '52032419':'正安县中观镇',
# '52032420':'正安县芙蓉江镇',
# '520325':'道真县',
# '52032501':'道真县尹珍街道',
# '52032502':'道真县大磏镇',
# '52032503':'道真县河口镇',
# '52032504':'道真县旧城镇',
# '52032505':'道真县洛龙镇',
# '52032506':'道真县隆兴镇',
# '52032507':'道真县平模镇',
# '52032508':'道真县上坝乡',
# '52032509':'道真县三江镇',
# '52032510':'道真县三桥镇',
# '52032511':'道真县桃源乡',
# '52032512':'道真县玉溪镇',
# '52032513':'道真县阳溪镇',
# '52032514':'道真县棕坪乡',
# '52032515':'道真县忠信镇',
# '520326':'务川县',
# '52032601':'务川县丰乐镇',
# '52032602':'务川县茅天镇',
# '52032603':'务川县蕉坝镇',
# '52032604':'务川县柏村镇',
# '52032605':'务川县浞水镇',
# '52032606':'务川县砚山镇',
# '52032607':'务川县泥高镇',
# '52032608':'务川县石朝乡',
# '52032609':'务川县分水镇',
# '52032610':'务川县镇南镇',
# '52032611':'务川县红丝乡',
# '52032612':'务川县涪洋镇',
# '52032613':'务川县黄都镇',
# '52032614':'务川县丹砂街道',
# '52032615':'务川县大坪街道',
# '52032616':'务川县都濡街道',
# '520327':'凤冈县',
# '52032701':'凤冈县永安镇',
# '52032702':'凤冈县凤岭街道',
# '52032703':'凤冈县琊川镇',
# '52032704':'凤冈县天桥镇',
# '52032705':'凤冈县绥阳镇',
# '52032706':'凤冈县土溪镇',
'52032707':'凤冈县王寨镇',
'52032708':'凤冈县龙泉街道',
'52032709':'凤冈县何坝街道',
'52032710':'凤冈县进化镇',
'52032711':'凤冈县蜂岩镇',
'52032712':'凤冈县新建镇',
'52032713':'凤冈县永和镇',
'52032714':'凤冈县花坪街道',
'520328':'湄潭县',
'52032801':'湄潭县抄乐镇',
'52032802':'湄潭县黄家坝街道',
'52032803':'湄潭县复兴镇',
'52032804':'湄潭县鱼泉街道',
'52032805':'湄潭县永兴镇',
'52032806':'湄潭县石莲镇',
'52032807':'湄潭县马山镇',
'52032808':'湄潭县洗马镇',
'52032809':'湄潭县新南镇',
'52032810':'湄潭县茅坪镇',
'52032811':'湄潭县天城镇',
'52032812':'湄潭县高台镇',
'52032813':'湄潭县湄江街道',
'52032814':'湄潭县西河镇',
'52032815':'湄潭县兴隆镇',
'520329':'余庆县',
'52032901':'余庆县子营街道',
'52032902':'余庆县关兴镇',
'52032903':'余庆县松烟镇',
'52032904':'余庆县构皮滩镇',
'52032905':'余庆县白泥镇',
'52032906':'余庆县敖溪镇',
'52032907':'余庆县大乌江镇',
'52032908':'余庆县龙溪镇',
'52032909':'余庆县龙家镇',
'52032910':'余庆县花山苗族乡',
'520330':'习水县',
'52033001':'习水县东皇街道',
'52033002':'习水县九龙街道',
'52033003':'习水县杉王街道',
'52033004':'习水县马临街道',
'52033005':'习水县民化镇',
'52033006':'习水县土城镇',
'52033007':'习水县同民镇',
'52033008':'习水县醒民镇',
'52033009':'习水县习酒镇',
'52033010':'习水县回龙镇',
'52033011':'习水县桑木镇',
'52033012':'习水县二郎镇',
'52033013':'习水县二里镇',
'52033014':'习水县仙源镇',
'52033015':'习水县官店镇',
'52033016':'习水县桃林镇',
'52033017':'习水县双龙乡',
'52033018':'习水县坭坝乡',
'52033019':'习水县寨坝镇',
'52033020':'习水县大坡镇',
'52033021':'习水县温水镇',
'52033022':'习水县程寨镇',
'52033023':'习水县良村镇',
'52033024':'习水县隆兴镇',
'52033025':'习水县三岔河镇',
'52033026':'习水县永安镇',
'520381':'赤水市',
'52038102':'赤水市复兴镇',
'52038103':'赤水市长沙镇',
'52038104':'赤水市天台镇',
'52038105':'赤水市石堡乡',
'52038106':'赤水市丙安镇',
'52038107':'赤水市元厚镇',
'52038108':'赤水市大同镇',
'52038109':'赤水市旺隆镇',
'52038110':'赤水市宝源乡',
'52038111':'赤水市长期镇',
'52038112':'赤水市官渡镇',
'52038113':'赤水市白云乡',
'52038114':'赤水市葫市镇',
'52038115':'赤水市金华街道',
'52038116':'赤水市文华街道',
'52038117':'赤水市两河口镇',
'52038118':'赤水市市中街道',
'520382':'仁怀市',
'52038201':'仁怀市高大坪镇',
'52038202':'仁怀市长岗镇',
'52038203':'仁怀市后山乡',
'52038204':'仁怀市中枢街道',
'52038205':'仁怀市苍龙街道',
'52038206':'仁怀市合马镇',
'52038207':'仁怀市茅台镇',
'52038208':'仁怀市火石镇',
'52038209':'仁怀市大坝镇',
'52038210':'仁怀市九仓镇',
'52038211':'仁怀市鲁班街道',
'52038212':'仁怀市龙井镇',
'52038213':'仁怀市茅坝镇',
'52038214':'仁怀市三合镇',
'52038215':'仁怀市美酒河镇',
'52038216':'仁怀市坛厂街道',
'52038217':'仁怀市学孔镇',
'52038218':'仁怀市喜头镇',
'52038219':'仁怀市盐津街道',
'52038220':'仁怀市五马镇',
'520383':'新蒲新区',
'52038301':'新蒲新区喇叭镇',
'52038302':'新蒲新区三渡镇',
'52038303':'新蒲新区虾子镇',
'52038304':'新蒲新区新舟镇',
'52038305':'新蒲新区永乐镇',
'52038306':'新蒲新区新蒲街道',
'52038307':'新蒲新区礼仪街道',
'52038308':'新蒲新区新中街道',
}
        for key, value in region.items():
            if len(key)<7:continue
            for i in item:
                # self.updata(item,code)
                # print(i)

                try:
                    返回结果=self.updata(i,key)
                    logging.info(返回结果)
                    print(返回结果)

                except ConnectionRefusedError:
                    sys.exit()
                    logging.info(f"目标计算机积极拒绝连接")

                    #print("目标计算机积极拒绝连接")
                    # 在这里处理连接被拒绝的情况
                except :

                    logging.info(f'{value}>{i}>****存在异常')
                    print(f'{value}>{i}>****存在异常')


# SetDispatch().updata('异地长期居住人员备案-登记','52030310',)
SetDispatch().loop()